package com.ribas.stephane.db;

/* 
 * Copyright (C) 2012 Stephane Ribas
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;

import com.ribas.stephane.R;

public class DatabaseHelper extends SQLiteOpenHelper {
	// variable pour le debug ;-)
	private static final String TAG = "StatusActivity";
	
	protected Context context;
	
	// BDD Name
	public static final String DATABASE_NAME = "bddhomeopathique";
	private static final int DATABASE_VERSION = 1;

	public DatabaseHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		this.context = context;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		/*
		 * Create the two tables and populate it with sample data.
		 */
		String s;
		try {
			Toast.makeText(context, "Loading...", 1500).show();
			InputStream in = context.getResources().openRawResource(R.raw.listeaffectionssql);
			DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
			Document doc = builder.parse(in, null);
			NodeList statements = doc.getElementsByTagName("statement");
			for (int i=0; i<statements.getLength(); i++) {
				s = statements.item(i).getChildNodes().item(0).getNodeValue();
				db.execSQL(s);
			}
			Log.d(TAG, "OnCreate - Affections First Time and uniq");
		} catch (Throwable t) {
			Toast.makeText(context, t.toString(), 90000).show();
		}
		
		String u;
		try {
			InputStream in = context.getResources().openRawResource(R.raw.listeremedessql);
			DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
			Document doc = builder.parse(in, null);
			NodeList statements = doc.getElementsByTagName("statement");
			for (int i=0; i<statements.getLength(); i++) {
				u = statements.item(i).getChildNodes().item(0).getNodeValue();
				db.execSQL(u);
			}
			Log.d(TAG, "OnCreate - Remedes First Time and uniq");
		} catch (Throwable t) {
			Toast.makeText(context, t.toString(), 90000).show();
		}

	}
	
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.d(TAG, "Upgrading database from version "
				+ oldVersion + " to " + newVersion
				+ ", which will destroy all old data");
		db.execSQL("DROP TABLE IF EXISTS listaffections");
		db.execSQL("DROP TABLE IF EXISTS listremedes");
		
		onCreate(db);
	}

}